<?php
/**
 * Created by PhpStorm.
 * User: hgs
 * Date: 16/7/18
 * Time: 上午11:48
 */

namespace C\lib {

    require LIB_PATH . 'Wxpaapi/lib/WxPay.Api.php';
    require LIB_PATH . 'Wxpaapi/lib/WxPay.JsApiPay.php';

    class Wxpay {
        
        public static function createOrder($opt = []) {
            //return $opt;
            $input = new \WxPayUnifiedOrder();
            $input->SetBody($opt['body']);
            $input->SetAttach($opt['attach']);
            $input->SetOut_trade_no($opt['out_trade_no']);
            $input->SetTotal_fee($opt['total_fee']);
            $input->SetTime_start(date("YmdHis"));
            // --- 20 分钟内（预留30秒）支付完毕 ---
            $input->SetTime_expire(date("YmdHis", $_SERVER['REQUEST_TIME'] + 1200 + 30));
            $input->SetGoods_tag('test');
            $input->SetNotify_url($opt['notify_url']);
            $input->SetTrade_type("JSAPI");
            $input->SetOpenid($opt['openid']);
            //var_dump($input);exit;
            $wxOrder = \WxPayApi::unifiedOrder($input);
            //var_dump($wxOrder);
            $tools = new \JsApiPay();
            $jsApiParameters = $tools->GetJsApiParameters($wxOrder);
            //$parameters=$tools->GetEditAddressParameters();

//            $data['a']=$jsApiParameters;
//            $data['b']=$parameters;
            return $jsApiParameters;
                //var_dump($jsApiParameters);exit;
            // --- 要 decode 一下否则是个字符串而不是 json 对象就呵呵哒了 ---
            //return json_decode($jsApiParameters, true);
            
        }

        public static function callback() {

            require LIB_PATH.'Wxpaapi/lib/WxPay.Notify.php';
            require LIB_PATH.'Wxpaapi/lib/WxPay.NotifyCallBack.php';
            $GLOBALS['HTTP_RAW_POST_DATA'] = isset($GLOBALS['HTTP_RAW_POST_DATA']) ? $GLOBALS['HTTP_RAW_POST_DATA'] : file_get_contents("php://input");
            $notify = new \WxPayNotifyCallBack();
            $notify->Handle(false);

        }

    }
    
}

